import java.awt.Color;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class gmmStrat extends JFrame{
	BufferedImage backbuffer, bg, player, mp;
	gmmStrat gm;
	int xcoord, ycoord;
	Color red = new Color(.9f, .1f, .1f, .7f);




	public gmmStrat(){
		setTitle("strat");
		gm = this;
		backbuffer = new BufferedImage(1280, 800, BufferedImage.TYPE_INT_RGB);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		try {
			bg = ImageIO.read(new File("Levels/15_bg.png"));
			player = ImageIO.read(new File("Levels/player.png"));
			mp = ImageIO.read(new File("Levels/moving_h.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(0);
		}
		setSize(1280, 800);

		setVisible(true);
		this.createBufferStrategy(2);

		Thread thrd = new Thread(new FrameDrawer() );
		thrd.setPriority(Thread.MAX_PRIORITY);
		thrd.start();




	}

	class FrameDrawer implements Runnable{

		int targetMsPerFrame = 16;
		long lastFrameTimeStamp = 0;

		public void run() {
			System.out.println("frameeee");
			while(true){
				lastFrameTimeStamp =  System.currentTimeMillis();

				drawAFrame();


				try {
					long slp = (long)16 - (System.currentTimeMillis() - lastFrameTimeStamp);

					if(slp >=0){
						System.out.println("slp:"+slp);
						Thread.sleep(slp);
					}else{
						//Uh oh, frames take longer to draw than our dT allows... aka can't keep up the framerate
					}
				} catch (InterruptedException e) {
					System.out.println("couldn't interrupt??");
					e.printStackTrace();

				}
				System.out.println("frame!");
			}
		}

		public void drawAFrame() {
			long ftime = System.currentTimeMillis();

			xcoord +=2;

			xcoord%=1280;
			ycoord%=800;


			drawStuff();


		}


		private void drawStuff() {
			System.out.println("drawStuff");
			BufferStrategy bf = gm.getBufferStrategy();
			Graphics g = null;
			if(bf!=null){
				g = bf.getDrawGraphics();	 

				g.drawImage(bg,0,0,null);

				g.setColor(red);
				g.fillRect(xcoord, ycoord, 160, 40);
				g.setColor(Color.black);
				g.drawRect(xcoord, ycoord, 160, 40);

				g.dispose();
				bf.show();

			}
			// It is best to dispose() a Graphics object when done with it.

			// Shows the contents of the backbuffer on the screen.

			//Tell the System to do the Drawing now, otherwise it can take a few extra ms until 
			//Drawing is done which looks very jerky
			Toolkit.getDefaultToolkit().sync();	
		}




	}


	public static void main(String[]args) {	new gmmStrat();}
}

